public class MyHash {

	static final int DEFAULT_INITIAL_CAPACITY = 16;
	static final int MAXIMUM_CAPACITY = 1 << 30;
	static final float DEFAULT_LOAD_FACTOR = 0.75f;

	transient Entry[] table;

	transient int size;

	int threshold;

	final float loadFactor;

	public MyHash(int initialCapacity, float loadFactor) {
		if (initialCapacity < 0)
			throw new IllegalArgumentException("Illegal initial capacity: "
					+ initialCapacity);
		if (initialCapacity > MAXIMUM_CAPACITY)
			initialCapacity = MAXIMUM_CAPACITY;
		if (loadFactor <= 0 || Float.isNaN(loadFactor))
			throw new IllegalArgumentException("Illegal load factor: "
					+ loadFactor);

		int capacity = 1;
		while (capacity < initialCapacity)
			capacity <<= 1;

		this.loadFactor = loadFactor;
		threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
		table = new Entry[capacity];
		init();
	}

	public MyHash(int initialCapacity) {
		this(initialCapacity, DEFAULT_LOAD_FACTOR);
	}

	public MyHash() {
		this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
	}

	void init() {
	}

	final int hash(int k) {
		return k;
	}

	static int indexFor(int h, int length) {
		return h & (length - 1);
	}

	public int size() {
		return size;
	}

	public boolean isEmpty() {
		return size == 0;
	}

	/**
	 * if the map is empty we return 0
	 */
	public int get(int key) {
		Entry entry = getEntry(key);
		return null == entry ? 0 : entry.getValue();

	}

	public boolean containsKey(int key) {
		return getEntry(key) != null;
	}

	final Entry getEntry(int key) {
		int hash = hash(key);
		for (Entry e = table[indexFor(hash, table.length)]; e != null; e = e.next) {
			int k;
			if (e.hash == hash && ((k = e.key) == key || (key == k)))
				return e;
		}
		return null;
	}

	public void put(int key, int value) {

		int hash = hash(key);
		int i = indexFor(hash, table.length);
		for (Entry e = table[i]; e != null; e = e.next) {
			int k;
			if (e.hash == hash && ((k = e.key) == key || key == k)) {
				e.value = value;
				return;
			}
		}
		addEntry(hash, key, value, i);
	}

	void resize(int newCapacity) {
		Entry[] oldTable = table;
		int oldCapacity = oldTable.length;
		if (oldCapacity == MAXIMUM_CAPACITY) {
			threshold = Integer.MAX_VALUE;
			return;
		}

		Entry[] newTable = new Entry[newCapacity];

		transfer(newTable, true);
		table = newTable;
		threshold = (int) Math.min(newCapacity * loadFactor,
				MAXIMUM_CAPACITY + 1);
	}

	void transfer(Entry[] newTable, boolean rehash) {
		int newCapacity = newTable.length;
		for (Entry e : table) {
			while (null != e) {
				Entry next = e.next;
				if (rehash) {
					e.hash = hash(e.key);
				}
				int i = indexFor(e.hash, newCapacity);
				e.next = newTable[i];
				newTable[i] = e;
				e = next;
			}
		}
	}

	public void remove(int key) {
		removeEntryForKey(key);
	}

	final Entry removeEntryForKey(int key) {
		int hash = hash(key);
		int i = indexFor(hash, table.length);
		Entry prev = table[i];
		Entry e = prev;

		while (e != null) {
			Entry next = e.next;
			int k;
			if (e.hash == hash && ((k = e.key) == key || key == k)) {

				size--;
				if (prev == e)
					table[i] = next;
				else
					prev.next = next;
				e.recordRemoval(this);
				return e;
			}
			prev = e;
			e = next;
		}
		return e;
	}

	public void clear() {
		Entry[] tab = table;
		for (int i = 0; i < tab.length; i++)
			tab[i] = null;
		size = 0;
	}

	static class Entry {
		final int key;
		int value;
		Entry next;
		int hash;

		Entry(int h, int k, int v, Entry n) {
			value = v;
			next = n;
			key = k;
			hash = h;
		}

		public final int getKey() {
			return key;
		}

		public final int getValue() {
			return value;
		}

		public final int setValue(int newValue) {
			int oldValue = value;
			value = newValue;
			return oldValue;
		}

		public final boolean equals(int o) {
			if (key == o)
				return true;

			return false;
		}

		public final int hashCode() {
			return key;
		}

		public final String toString() {
			return getKey() + "=" + getValue();
		}

		/**
		 * This method is invoked whenever the entry is removed from the table.
		 */
		void recordRemoval(MyHash m) {
		}
	}

	void addEntry(int hash, int key, int value, int bucketIndex) {
		if ((size >= threshold) && (null != table[bucketIndex])) {
			resize(2 * table.length);
			hash = hash(key);
			bucketIndex = indexFor(hash, table.length);
		}
		createEntry(hash, key, value, bucketIndex);
	}

	void createEntry(int hash, int key, int value, int bucketIndex) {
		Entry e = table[bucketIndex];
		table[bucketIndex] = new Entry(hash, key, value, e);
		size++;
	}

    /**
     * main method is to test MyHash
     */	
	public static void main(String[] args) {
		MyHash mh = new MyHash();


		System.out.println("test 1");
		for (int i = 0; i < 150; i++) {
			mh.put(i, i + 1);
		}

		for (int i = 0; i < 150; i++) {
			System.out.print(mh.get(i) + " ");
		}

		System.out.println("\ntest 2");
		mh.clear();

		for (int i = 0; i < 10; i++) {
			System.out.print(mh.get(i) + " ");
		}

		System.out.println("\ntest 3");
		mh.put(9, 10);
		System.out.print(mh.get(11));	
		

	}

}
